<template>
  <div class="app-container">
        <el-header>
          <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect" style="float:right">
            <el-menu-item index="1" @click="toFrontIndexPage">首页</el-menu-item>
            <el-menu-item index="2" v-if="isLogin==false"  @click="toLoginPage">登录</el-menu-item>
            <el-menu-item index="3" v-if="isLogin==false" @click="toRegisterPage">注册</el-menu-item>
            <el-submenu v-if="isLogin==true" index="4">
              <template v-if="isLogin==true" slot="title">个人中心</template>
              <el-menu-item @click="toFrontUserProfilePage"  v-if="isLogin==true" index="4-1">基本信息</el-menu-item>
              <el-menu-item @click="toFrontRecordListPage"  v-if="isLogin==true" index="4-2">生成记录</el-menu-item>
              <el-menu-item @click.native="logout" v-if="isLogin==true" index="4-3">Logout</el-menu-item>
            </el-submenu>
          </el-menu>
        </el-header>


    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基本资料</span>
          </div>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="user" />
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import userAvatar from "./userAvatar";
import userInfo from "./userInfo";
import resetPwd from "./resetPwd";
import { getUserProfile } from "@/api/system/user";
import { getToken } from '@/utils/auth'
export default {

  name: "Profile",
  components: { userAvatar, userInfo, resetPwd },
  data() {
    return {
      user: {},
      roleGroup: {},
      postGroup: {},
      activeTab: "userinfo",

      activeIndex: '1',
      activeIndex2: '2',

      isLogin :("undefined".indexOf(getToken())!==-1) ? false :true
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
      });
    },
          toLoginPage(){
            this.$router.push("/login" );
          },
          toRegisterPage(){
            this.$router.push("/register" );
          },
          toFrontIndexPage(){
            this.$router.push("/" );
          },
          toFrontUserProfilePage(){
            this.$router.push("/front/user" );
          },
          toFrontRecordListPage(){
            this.$router.push("/front/frontRecord" );
          },

          async logout() {
            this.$confirm('确定注销并退出系统吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$store.dispatch('LogOut').then(() => {
                location.href = '/index';
              })
            }).catch(() => {});
          }
  }
};
</script>
